/*
 * @Author: xiehangyun 
 * @Date: 2025-05-16 18:15:40 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-09-04 19:08:58
 */

import { makeAutoObservable } from "mobx";
import { PromotionData } from "types/type";
import promoteRelationshipsApi from "./promoteRelationshipsApi";
import dayjs from "dayjs";

class PromoteRelationshipsStore {

    constructor() {
        makeAutoObservable(this, {}, { deep: false });
    }

    // 提现记录
    pageNum = 1;
    pageSize = 10;
    year?: number;
    month?: number;
    day?: number;
    relationsList: Array<PromotionData> = [];
    hasMore = true; // 是否有下一页
    total = 0;
    loading = false; // 是否正在加载中

    setDate(year: number, month: number, day: number) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.getNextRelations(true);
    }

    getNextRelations(reset = false) {

        console.log("hasMore", this.hasMore, "loading", this.loading);

        if (reset) {
            console.log(1);
            this.pageNum = 1;
            this.hasMore = true;
        } else if (!this.hasMore || this.loading) {
            console.log(2);
            return;
        } else {
            console.log(3);
            this.pageNum++;
        }
        console.log(4);
        this.loading = true;

        promoteRelationshipsApi.getChildrenList({
            pageNum: this.pageNum,
            pageSize: this.pageSize,
        }).then(async res => {

            //res.rows 按照 linkTime 排序 linkTime 是字符串的时间 要用 dayjs 转成时间戳
            if (res.rows.length >= 2) {
                res.rows = res.rows.sort((a, b) => {
                    return dayjs(b.linkTime).unix() - dayjs(a.linkTime).unix();
                });
            }

            if (reset) {
                this.relationsList = [];
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                this.relationsList = [...res.rows];
            } else {
                this.relationsList = [
                    ...this.relationsList,
                    ...res.rows,
                ];
            }
            this.total = res.total;
            this.hasMore = this.relationsList.length < res.total;
            await new Promise(resolve => setTimeout(resolve, 100));
            this.loading = false;
        });
    }

    // 查询父级推广人信息
    firstParentInfo?: PromotionData;
    async getFirstParentInfo() {
        const res = await promoteRelationshipsApi.getFirstParentInfo();
        this.firstParentInfo = res.data;
    }

    //编辑下级备注接口
    updateRemarkName(targetOpenId: string, remarkName: string) {
        promoteRelationshipsApi.updateRemarkName({ targetOpenId, remarkName }).then(res => {
            if (res.code === 200) {
                //直接编辑
                this.relationsList = this.relationsList.map(item => {
                    if (item.openId === targetOpenId) {
                        item.remarkName = remarkName;
                    }
                    return item;
                })
            }
        })
    }

};

const promoteRelationshipsStore = new PromoteRelationshipsStore();

export default promoteRelationshipsStore;